#include "app.h"

u8	RxdErrorCnt = 0;
u8	RxdCnt = 0;
u8	RxdFlag = 0;
u8	RxdData[8] = {0};
u8	TxdCnt = 0;
u8	TxdFlag = 0;
u8	TxdData[8] = {0};

u8	Mode;

u8	Temp_Water_Value_H,Temp_Water_Value_L;
u8	Temp_Seat_Value_H,Temp_Seat_Value_L;

u8	Kick_Status;

u8	Flag_AdStart,Ad_Count,Ad_EnvirLight;
u16	AdLight;

u8 	Flag_IR_Handle;

u8	IRDataArray[4];
u8	IR_CRC_Data;
u8	IR_ReadBitCnt;
u8	IR_High_Cnt;

u8	IRauxDataArray[4];
u8	IRaux_ReadBitCnt;
u8	Foot_IRaux_High_Cnt;
u16	Foot_IRaux_Low_Cnt;

u8	OverTimeCnt;
u8	IRHandleCnt;

u8	IRData,IRauxData;
u8	IRDataCnt,IRauxDataCnt;

u8	Flag_IR_ReadEnd;
u8	Flag_IRaux_ReadEnd;

u8	Temp_IR_Data[4];

//u8	Flag_DC_Battery;
//u8	LED_DC_BATTERY;

u8 	i;
u8	Water_Value_H,Water_Value_L,Seat_Value_H,Seat_Value_L;

bit BIT_TMP;

bit LED_POWER;
bit LED_AUTOFULSH;
bit LED_SIGNAL_SEAT;
bit LED_MODE_CHILD;
bit LED_MODE_OLDMAN;
bit LED_TEMP_WATER;
bit LED_TEMP_SEAT;
bit LED_DC_BATTERY;

bit Flag_DC_Battery;

void MODIFY_HIRC_16(void)
{
    unsigned char data hircmap0,hircmap1;
/* Check if power on reset, modify HIRC */
    if (PCON&SET_BIT4)
    {
        set_CHPCON_IAPEN;
        IAPAL = 0x30;
        IAPAH = 0x00;
        IAPCN = READ_UID;
        set_IAPTRG_IAPGO;
        hircmap0 = IAPFD;
        IAPAL = 0x31;
        IAPAH = 0x00;
        set_IAPTRG_IAPGO;
        hircmap1 = IAPFD;
        clr_CHPCON_IAPEN;
        TA=0XAA;
        TA=0X55;
        RCTRIM0 = hircmap0;
        TA=0XAA;
        TA=0X55;
        RCTRIM1 = hircmap1;
        clr_CHPCON_IAPEN;
    }
}
void MODIFY_HIRC_24(void)
{
    unsigned char data hircmap0,hircmap1;
/* Check if power on reset, modify HIRC */
    if (PCON&SET_BIT4)
    {
        set_CHPCON_IAPEN;
        IAPAL = 0x38;
        IAPAH = 0x00;
        IAPCN = READ_UID;
        set_IAPTRG_IAPGO;
        hircmap0 = IAPFD;
        IAPAL = 0x39;
        IAPAH = 0x00;
        set_IAPTRG_IAPGO;
        hircmap1 = IAPFD;
        clr_CHPCON_IAPEN;
        TA=0XAA;
        TA=0X55;
        RCTRIM0 = hircmap0;
        TA=0XAA;
        TA=0X55;
        RCTRIM1 = hircmap1;
        clr_CHPCON_IAPEN;
    }
}

void WDT_Reset_Init(void)
{
	TA=0xAA;
	TA=0x55;
	WDCON=0x03;                 //Setting WDT prescale,WDT will reset if  not clear WDCLK within 100ms
	set_WDCON_WDCLR;
}

void SegDisplay(u8 Num)
{
	switch(Num)	
	{
		case 0:
			SEG_A=0;
			SEG_B=0;
			SEG_C=0;
			SEG_D=0;
			SEG_E=0;
			SEG_F=0;
			SEG_G=1;
			SEG_DOT=0;
			break;
		case 1:
			SEG_A=1;
			SEG_B=0;
			SEG_C=0;
			SEG_D=1;
			SEG_E=1;
			SEG_F=1;
			SEG_G=1;
			SEG_DOT=0;
			break;
		case 2:
			SEG_A=0;
			SEG_B=0;
			SEG_C=1;
			SEG_D=0;
			SEG_E=0;
			SEG_F=1;
			SEG_G=0;
			SEG_DOT=0;
			break;			
		case 3:
			SEG_A=0;
			SEG_B=0;
			SEG_C=0;
			SEG_D=0;
			SEG_E=1;
			SEG_F=1;
			SEG_G=0;
			SEG_DOT=0;
			break;
		case 4:
			SEG_A=1;
			SEG_B=0;
			SEG_C=0;
			SEG_D=1;
			SEG_E=1;
			SEG_F=0;
			SEG_G=0;
			SEG_DOT=0;
			break;
		case 5:
			SEG_A=0;
			SEG_B=1;
			SEG_C=0;
			SEG_D=0;
			SEG_E=1;
			SEG_F=0;
			SEG_G=0;
			SEG_DOT=0;
			break;
		case 6:
			SEG_A=0;
			SEG_B=1;
			SEG_C=0;
			SEG_D=0;
			SEG_E=0;
			SEG_F=0;
			SEG_G=0;
			SEG_DOT=0;
			break;
		case 7:
			SEG_A=0;
			SEG_B=0;
			SEG_C=0;
			SEG_D=1;
			SEG_E=1;
			SEG_F=1;
			SEG_G=1;
			SEG_DOT=0;
			break;
		case 8:			
			SEG_A=0;
			SEG_B=0;
			SEG_C=0;
			SEG_D=0;
			SEG_E=0;
			SEG_F=0;
			SEG_G=0;
			SEG_DOT=0;
			break;
		case 9:
			SEG_A=0;
			SEG_B=0;
			SEG_C=0;
			SEG_D=0;
			SEG_E=1;
			SEG_F=0;
			SEG_G=0;
			SEG_DOT=0;
			break;	
		case 0x0a:	//"NULL"
			SEG_A=1;
			SEG_B=1;
			SEG_C=1;
			SEG_D=1;
			SEG_E=1;
			SEG_F=1;
			SEG_G=1;
			SEG_DOT=1;
			break;
		default:		//"--"
			SEG_A=1;
			SEG_B=1;
			SEG_C=1;
			SEG_D=1;
			SEG_E=1;
			SEG_F=1;
			SEG_G=0;
			SEG_DOT=0;
			break;
	}						
}


void SegScan(void)
{	
	MODEL_A=1;
	SEAT_A1=1;
	SEAT_A2=1;
	WATER_A1=1;
	WATER_A2=1;
	if(LED_DC_BATTERY)	
	{
		//_enadc=0;			//close the ad
		Flag_DC_Battery=1;	//close the seg						
	}
	else
	{
		//_enadc=1;			//open the ad
		Flag_DC_Battery=0;	//open the seg	
	}

	if((Temp_Seat_Value_H==0)&&(Temp_Seat_Value_L==0)&&(Temp_Seat_Value_H==0)&&(Temp_Seat_Value_L==0)&&(Mode==0))
	{
		Temp_Water_Value_H=8;
		Temp_Water_Value_L=8;
		Temp_Seat_Value_H=8;
		Temp_Seat_Value_L=8;
		Mode=0x7f;
	}
		
	if((Temp_Water_Value_H==0)&&(Temp_Water_Value_L==0))
	{
		Temp_Water_Value_H=0x0b;
		Temp_Water_Value_L=0x0b;
	}
													
	if(((Temp_Seat_Value_H==0)&&(Temp_Seat_Value_L==2))||((Temp_Seat_Value_H==0)&&(Temp_Seat_Value_L==0)))
	{
		Temp_Seat_Value_H=0x0b;
		Temp_Seat_Value_L=0x0b;
	}
	
	i++;
	if(Flag_DC_Battery==0)
	{
		Water_Value_H=Temp_Water_Value_H;
		Water_Value_L=Temp_Water_Value_L;
		Seat_Value_H=Temp_Seat_Value_H;
		Seat_Value_L=Temp_Seat_Value_L;
		Mode &= 0xFF;
	}
	else
	{
		Water_Value_H=0x0a;
		Water_Value_L=0x0a;
		Seat_Value_H=0x0a;
		Seat_Value_L=0x0a;
		Mode &= 0x81;
	}
	
	switch(i)
	{	
		case 1:
			MODEL_A=1;
			SegDisplay(Water_Value_H);
			WATER_A1=0;
			break;
		case 2:
			WATER_A1=1;
			SegDisplay(Water_Value_L);
			WATER_A2=0;
			break;
		case 3:
			WATER_A2=1;
			SegDisplay(Seat_Value_H);
			SEAT_A1=0;
			break;
		case 4:
			SEAT_A1=1;
			SegDisplay(Seat_Value_L);
			SEAT_A2=0;
			break;
		case 5:
			SEAT_A2=1;	
			//SEG_A=0;
			//SEG_B=0;
			//SEG_C=0;
			//SEG_D=0;
			//SEG_E=0;
			//SEG_F=0;
			//SEG_G=0;
			MODEL_A=0;
			i=0;
		default:
			break;	
	}	
}

void Uart_Rxd_Handle(void)
{
	if(RxdFlag)
	{
		RxdFlag = 0;
		if(RxdData[6]==(u8)(RxdData[0]+RxdData[1]+RxdData[2]+RxdData[3]+RxdData[4]+RxdData[5]))
		{
			Temp_Water_Value_H = RxdData[1];
			Temp_Water_Value_L = RxdData[2];
			Temp_Seat_Value_H = RxdData[3];
			Temp_Seat_Value_L = RxdData[4];
			Mode = RxdData[5];
			if(LED_DC_BATTERY)		
				Flag_DC_Battery=1;
		}

		TxdData[0]=0xbb;
		TxdData[1]=Ad_EnvirLight;
		TxdData[2]=Temp_IR_Data[0];
		TxdData[3]=Temp_IR_Data[1];
		TxdData[4]=Temp_IR_Data[2];
		TxdData[5]=Temp_IR_Data[3];
		TxdData[6]=Kick_Status;
		TxdData[7]=TxdData[0]+TxdData[1]+TxdData[2]+TxdData[3]+TxdData[4]+TxdData[5]+TxdData[6];
		Temp_IR_Data[0] = 0;
		Temp_IR_Data[1] = 0;
		Temp_IR_Data[2] = 0;
		Temp_IR_Data[3] = 0;
		Kick_Status = 0;
		TxdFlag = 1;
#ifdef	UART0		
		clr_IE_ES;	/* Disable UART0 interrupt */
#else		
		clr_EIE1_ES_1;	/* Disable UART1 interrupt */
#endif		
	}
}
void Uart_Txd_Handle(void)
{
	if(TxdFlag)
	{
		TxdFlag = 0;
#ifdef	UART0		
		Uart0_Send(TxdData,8);
		set_IE_ES;	/* Enable UART0 interrupt */
#else		
		Uart1_Send(TxdData,8);
		set_EIE1_ES_1;	/* Enable UART1 interrupt */
#endif		
	}
}

void Uart0_Send(u8 *Sp,u8 Byte)
{
	u8 i;
	for(i=0;i<Byte;i++)
	{
		TI = 0;
		SBUF = *Sp;
        	while(TI==0);
		Sp ++;
	}
}

void Uart1_Send(u8 *Sp,u8 Byte)
{
	u8 i;
	for(i=0;i<Byte;i++)
	{
		TI_1 = 0;
		SBUF_1 = *Sp;
        	while(TI_1==0);
		Sp ++;
	}
}

void Uart_Error_Handle(void)
{	
	if(RxdCnt)
		RxdErrorCnt ++;
	else
		RxdErrorCnt = 0;

	if(RxdErrorCnt > 20)
	{
		RxdErrorCnt = 0;
		RxdCnt = 0; 
	}
}

void IR_Handle(void)
{
	if(IRDataCnt == 4)
	{
		Flag_IR_ReadEnd = 1;
		IRDataCnt = 0;
	}

	if(IRauxDataCnt == 4)
	{
		Flag_IRaux_ReadEnd = 1;
		IRauxDataCnt = 0;
	}

	if(IR_Foot_Pin)
	{
		if(Foot_IRaux_Low_Cnt > FOOT_LONG_EFFECT_CNT)
			Kick_Status = 2;
		else if(Foot_IRaux_Low_Cnt > FOOT_SHORT_EFFECT_CNT)
			Kick_Status = 1;

		Foot_IRaux_Low_Cnt = 0;
	}
	IR_Service();
}

void IR_Service(void)
{
//normal function------------------------------------
	if(Flag_IR_ReadEnd && !Flag_IR_Handle)
	{
		Flag_IR_ReadEnd=0;
		if((IRDataArray[0] >= 0x2a) && (IRDataArray[0] <= 0x7a))
		{
			IR_CRC_Data=0;
			IR_CRC_Data += IRDataArray[0]>>4;
			IR_CRC_Data += IRDataArray[0]&0x0F;
			IR_CRC_Data += IRDataArray[1]>>4;
			IR_CRC_Data += IRDataArray[1]&0x0F;
			IR_CRC_Data += IRDataArray[2]>>4;
			IR_CRC_Data += IRDataArray[2]&0x0F;
			IR_CRC_Data += IRDataArray[3]>>4;
			IR_CRC_Data &= 0x0F;
		}
		if(IR_CRC_Data==(IRDataArray[3]&0x0F))
		{
			Flag_IR_Handle=1;
			Flag_IRaux_ReadEnd = 0;
			Temp_IR_Data[0] = IRDataArray[0];
			Temp_IR_Data[1] = IRDataArray[1];
			Temp_IR_Data[2] = IRDataArray[2];
			Temp_IR_Data[3] = IRDataArray[3];
		}
	}
	
	if(Flag_IRaux_ReadEnd && !Flag_IR_Handle)
	{
		Flag_IRaux_ReadEnd = 0;
		if((IRauxDataArray[0] >= 0x2a) && (IRauxDataArray[0] <= 0x7a))
		{
			IR_CRC_Data = 0;
			IR_CRC_Data += IRauxDataArray[0]>>4;
			IR_CRC_Data += IRauxDataArray[0]&0x0F;
			IR_CRC_Data += IRauxDataArray[1]>>4;
			IR_CRC_Data += IRauxDataArray[1]&0x0F;
			IR_CRC_Data += IRauxDataArray[2]>>4;
			IR_CRC_Data += IRauxDataArray[2]&0x0F;
			IR_CRC_Data += IRauxDataArray[3]>>4;
			IR_CRC_Data &= 0x0F;
		}
		if(IR_CRC_Data==(IRauxDataArray[3]&0x0F))
		{
			Flag_IR_Handle=1;
			Flag_IR_ReadEnd = 0;
			Temp_IR_Data[0] = IRauxDataArray[0];
			Temp_IR_Data[1] = IRauxDataArray[1];
			Temp_IR_Data[2] = IRauxDataArray[2];
			Temp_IR_Data[3] = IRauxDataArray[3];
		}
	}
}

void IROverTimeHandle(void)
{		
	if(Flag_IR_Handle)
		IRHandleCnt ++;
	else
		IRHandleCnt = 0;
	if(IRHandleCnt > 60)
	{
		Flag_IR_Handle = 0;
	}
	
	if(IRDataCnt || IRauxDataCnt)
		OverTimeCnt ++;
	else
		OverTimeCnt = 0;
	if(OverTimeCnt >= 10)
	{
		IRDataCnt = 0;
		IRauxDataCnt = 0;
	}
}

void Ad_Service(void)
{
	if(!Flag_AdStart)
	{
		AdLight = 0;
		clr_ADCCON0_ADCF;
      		set_ADCCON0_ADCS;                  // ADC start trig signal
      		Flag_AdStart = 1;
	}
	else
	{
		Ad_Count ++;
		if(Ad_Count<=16)
		{
			AdLight += ADCRH;	
			clr_ADCCON0_ADCF;
	      		set_ADCCON0_ADCS;                  // ADC start trig signal
		}
		else
		{
			Ad_Count=0;
			Ad_EnvirLight = AdLight>>4;	
			Flag_AdStart = 0;
		}
	}
}

